﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class RotateRule : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected HtmlForm F2181;
        protected GridView gvRotationRuleDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRotateRuleID;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlHead Head1;
        protected Label lblArea;
        protected Label lblDescr;
        protected Label lblMsg;
        protected Label lblRotationRuleID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtRotationRuleID;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtRotationRuleID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_002");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnRotateRuleID.Value))
                {
                    try
                    {
                        this.InsertRotationInfo();
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2420_004") + ex.Message;
                    }
                }
                base.Response.Redirect(string.Format("RotateRuleDetail.aspx?ID={0}&SeqNo={1}&from={2}", base.UrlEncode(this.txtRotationRuleID.Text), base.UrlEncode(this.hdnSeqNo.Value), base.UrlEncode(this.hdnFrom.Value)));
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtRotationRuleID.Text = "";
            this.txtRotationRuleID.Enabled = true;
            this.hdnRotateRuleID.Value = "";
            this.chkSysCreated.Checked = false;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C011");
            }
            else if (!string.IsNullOrEmpty(this.txtRotationRuleID.Text.Trim()))
            {
                try
                {
                    List<string> RotationRule = new List<string> {
                        this.txtRotationRuleID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(RotationRule, "ROTATION_RULE") + base.GetGlobalMsgResource("C006");
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtRotationRuleID.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                try
                {
                    List<string> RotationRuleID = base.GetSelectedIDsInGridView(this.gvRotationRuleDetail, "RotationRuleID");
                    List<string> LotAttributes = base.GetSelectedIDsInGridView(this.gvRotationRuleDetail, "LotAttributes");
                    if ((LotAttributes == null) || (LotAttributes.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                        return;
                    }
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(RotationRuleID, LotAttributes, new List<string>(), "ROTATION_RULE_DETAIL") + base.GetGlobalMsgResource("C006");
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
                this.SetSysCodeInfo3Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_005");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("RotateRuleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("RotateRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtRotationRuleID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_002");
                this.txtRotationRuleID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtDescr.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_003");
                this.txtDescr.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateRotationInfo();
            }
            else
            {
                this.InsertRotationInfo();
            }
        }

        protected void gvRotationRuleDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2181", this.gvRotationRuleDetail);
        }

        protected void gvRotationRuleDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.PageQueryEntitySession as RotationRuleInfo).RotationRuleDetailInfo;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvRotationRuleDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvRotationRuleDetail.PageIndex * this.gvRotationRuleDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvRotationRuleDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                RotationRuleInfo rotationRuleInfo = base.DataEntityViewState as RotationRuleInfo;
                DataView dv = CADataConverter.ToDataTable(rotationRuleInfo.RotationRuleDetailInfo).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvRotationRuleDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertRotationInfo()
        {
            try
            {
                RotationRuleInfo rotationRule = new RotationRuleInfo();
                if (base.DataEntityViewState != null)
                {
                    rotationRule = base.DataEntityViewState as RotationRuleInfo;
                    if ((rotationRule.RotationRuleDetailInfo != null) && (rotationRule.RotationRuleDetailInfo.Count > 0))
                    {
                        for (int i = 0; i < rotationRule.RotationRuleDetailInfo.Count; i++)
                        {
                            rotationRule.RotationRuleDetailInfo[i].RotationRuleID = this.txtRotationRuleID.Text.Trim();
                        }
                    }
                }
                rotationRule.RotationRuleID = this.txtRotationRuleID.Text;
                rotationRule.Descr = this.txtDescr.Text.Trim();
                rotationRule.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                rotationRule.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                rotationRule.CreatedBy = base.UserInfo.UserID;
                this.hdnRotateRuleID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(rotationRule, "ROTATION_RULE");
                this.lblMsg.Text = this.hdnRotateRuleID.Value + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = rotationRule;
                this.hdnFlag.Value = "1";
                base.PageQueryEntitySession = rotationRule;
                this.txtRotationRuleID.Enabled = false;
                if ((rotationRule.RotationRuleDetailInfo != null) && (rotationRule.RotationRuleDetailInfo.Count > 0))
                {
                    this.hdnSeqNo.Value = (rotationRule.RotationRuleDetailInfo[rotationRule.RotationRuleDetailInfo.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnSeqNo.Value = "10";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnRotateRuleID.Value = base.Request.QueryString["ID"].ToString();
                this.SetRotateRuleInfo2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
            if (base.Request.QueryString["type"] == "detail")
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetControlsGvRotationRuleData(RotationRuleInfo RotationRule)
        {
            if (RotationRule.RotationRuleDetailInfo.Count > 0)
            {
                this.gvRotationRuleDetail.DataSource = RotationRule.RotationRuleDetailInfo;
                this.gvRotationRuleDetail.DataBind();
                base.DataEntityViewState = RotationRule;
            }
        }

        private void SetControlsRotationRuleText(RotationRuleInfo RotationRule)
        {
            if (RotationRule != null)
            {
                this.txtRotationRuleID.Text = RotationRule.RotationRuleID;
                this.txtDescr.Text = RotationRule.Descr;
                this.chkIsActive.Checked = RotationRule.IsActive == "Y";
                this.chkSysCreated.Checked = RotationRule.SystemCreated == "Y";
                if ((RotationRule.RotationRuleDetailInfo != null) && (RotationRule.RotationRuleDetailInfo.Count > 0))
                {
                    this.hdnSeqNo.Value = (RotationRule.RotationRuleDetailInfo[RotationRule.RotationRuleDetailInfo.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnSeqNo.Value = "10";
                }
            }
        }

        private void SetRotateRuleInfo2Form()
        {
            try
            {
                string rotateRuleID = this.hdnRotateRuleID.Value;
                RotationRuleInfo rotateRule = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(rotateRuleID, "ROTATION_RULE") as RotationRuleInfo;
                base.PageQueryEntitySession = rotateRule;
                this.SetControlsRotationRuleText(rotateRule);
                this.SetControlsGvRotationRuleData(rotateRule);
                this.txtRotationRuleID.Enabled = false;
                if (this.chkSysCreated.Checked)
                {
                    this.chkIsActive.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetSysCodeInfo3Form()
        {
            RotationRuleInfo rotationRule = base.PageQueryEntitySession as RotationRuleInfo;
            RotationRuleInfo RotationRule = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(rotationRule.RotationRuleID, "ROTATION_RULE") as RotationRuleInfo;
            this.hdnRotateRuleID.Value = RotationRule.RotationRuleID;
            base.PageQueryEntitySession = RotationRule;
            this.SetControlsRotationRuleText(RotationRule);
            base.BindData2GridView(this.gvRotationRuleDetail, RotationRule.RotationRuleDetailInfo);
        }

        private void UpdateRotationInfo()
        {
            try
            {
                RotationRuleInfo rotationRule = new RotationRuleInfo();
                rotationRule.RotationRuleID = this.txtRotationRuleID.Text;
                rotationRule.Descr = this.txtDescr.Text.Trim();
                rotationRule.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                rotationRule.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                rotationRule.Descr = this.txtDescr.Text.Trim();
                rotationRule.CreatedBy = "";
                rotationRule.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(rotationRule, "ROTATION_RULE") + base.GetGlobalMsgResource("C002");
                base.PageQueryEntitySession = rotationRule;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

